package com.bear.newsset.widget;

import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.MotionEvent;

import com.bear.newsset.util.AppUtil;
import com.bear.newsset.util.LogUtils;
import com.bear.newsset.util.SysUtil;
import com.bear.newsset.util.Utils;
import com.ybear.ybmvp.view.YBAppCompatActivity;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;

public class BaseActivity extends YBAppCompatActivity {
    public enum STATUS_BAR_ICON_COLOR {
        WHITE, BLACK
    }

    //默认禁用滑动返回
    private boolean enableSwipeBack = true;
    //侧滑退出用到的坐标
    private float oldMoveX, oldMoveY;
    //侧滑退出用到的Touch下标
    private int touchIndex;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //默认禁止横屏
        setRequestedOrientation(
                setEnableLandscape() ? SCREEN_ORIENTATION_LANDSCAPE : SCREEN_ORIENTATION_PORTRAIT
        );
        //设置状态栏图标颜色
        setStatusBarIconColor(
                onStatusBarIconColor().equals(STATUS_BAR_ICON_COLOR.BLACK)
        );
        //添加该页面到退出栈
        AppUtil.addExitStack( this );
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppUtil.removeExitStack( this );
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        onSwipeBackTouchEvent( event );
        return super.onTouchEvent(event);
    }

    /**
     * 启用/禁用滑动返回
     * 侧滑返回没反应时需要在最上层（已显示）的View去调用{@link #onTouchEvent(MotionEvent)}
     * 并调用{@link #onSwipeBackTouchEvent(MotionEvent)}
     * @param enable    是否启用
     */
    public void setEnableSwipeBack(boolean enable) {
        enableSwipeBack = enable;
    }

    /**
     * 侧滑退出用到的Touch监听
     * @param ev    触摸事件
     */
    protected void onSwipeBackTouchEvent(MotionEvent ev) {
        if( !enableSwipeBack ) return;
        switch ( ev.getAction() ) {
            case MotionEvent.ACTION_DOWN:
                oldMoveX = -1;
                oldMoveY = ev.getY();
                touchIndex = 0;
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float y = ev.getY();
                int limits = SysUtil.getScreenWidth( this ) / 3;

                //x坐标必须递增的，不能超出指定范围，y坐标的幅度不能太大
                if( x > oldMoveX && x <= limits && Math.abs( y - oldMoveY ) <= 80 ) {
                    touchIndex++;
                } else {
                    MotionEvent e = MotionEvent.obtain( ev );
                    e.setAction( MotionEvent.ACTION_DOWN );
                    onSwipeBackTouchEvent( e );
                    break;
                }
                oldMoveX = x;
                break;
            case MotionEvent.ACTION_UP:
                LogUtils.d("onSwipeBackTouchEvent", "touchIndex:" + touchIndex);
                if( touchIndex >= 8 ) finish();
                break;
        }
    }

    /**
     * 设置状态栏图标颜色
     * @param isBlack   是否为黑色
     */
    protected  void setStatusBarIconColor(boolean isBlack) {
        //沉浸式状态栏
        Utils.immersiveStatusBar(this, getWindow());
        //设置状态栏颜色
        Utils.setStatusBarIconColor(getWindow(), isBlack);
    }

    /**
     * 设置状态栏图标颜色
     * @return      黑色图标： {@link STATUS_BAR_ICON_COLOR#BLACK}
     *              白色图标： {@link STATUS_BAR_ICON_COLOR#WHITE}
     */
    protected STATUS_BAR_ICON_COLOR onStatusBarIconColor() {
        return STATUS_BAR_ICON_COLOR.BLACK;
    }

    /**
     * 设置是否启用横屏
     */
    private boolean setEnableLandscape() {
        return false;
    }
}